16. 答案:TensorFlow 卷积层

方案

这是我的做法。注意:有不止一种方法得到正确的输出维度,你的答案可能会跟我的有所不同。

def conv2d(input):
    # Filter (weights and bias)
    F_W = tf.Variable(tf.truncated_normal((2, 2, 1, 3)))
    F_b = tf.Variable(tf.zeros(3))
    strides = [1, 2, 2, 1]
    padding = 'VALID'
    return tf.nn.conv2d(input, F_W, strides, padding) + F_b

我想要把输入的 (1, 4, 4, 1) 转变成 (1, 2, 2, 3)。padding 方法我选择 'VALID'。我觉得这更容易理解,也得到了我想要的结果。

out_height = ceil(float(in_height - filter_height + 1) / float(strides[1]))
out_width  = ceil(float(in_width - filter_width + 1) / float(strides[2]))

把值带入

out_height = ceil(float(4 - 2 + 1) / float(2)) = ceil(1.5) = 2
out_width  = ceil(float(4 - 2 + 1) / float(2)) = ceil(1.5) = 2

要把深度从 1 变成 3。我要把我滤波器的输出做相应的设置:

F_W = tf.Variable(tf.truncated_normal((2, 2, 1, 3))) # (height, width, input_depth, output_depth)
F_b = tf.Variable(tf.zeros(3)) # (output_depth)

输入的深度是 1,所以我选择 1 作为滤波器的 input_depth